package com.demo20_有效的括号;
/**
 * 通过!!!!!!
 *
 */

import java.util.Stack;

public class Solution {
    public static void main(String[] args) {

    }
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i)=='('||s.charAt(i)=='['||s.charAt(i)=='{'){
                stack.push(s.charAt(i));
            }else {
                if (!stack.isEmpty()){
                    Character peek = stack.peek();
                    if (peek==revers(s.charAt(i))){
                        stack.pop();
                    }else {
                        stack.push(s.charAt(i));
                    }
                }else {
                    stack.push(s.charAt(i));
                }

            }
        }

        return stack.isEmpty();
    }

    public Character revers(Character c){
        switch (c){
            case ')':
                return '(';
            case ']':
                return '[';
            case '}':
                return '{';
            default:
                return c;
        }

    }
}
